
#ifndef _WICOM_H_ 
#define _WICOM_H_

#include "windows.h"
#include "stdio.h"
#include "string.h"
#pragma comment(lib, "winmm.lib")
#define MAXWIDATALENGHT 640
#define MAXCYCLEBUFCELLLENGHT 640
#define CYCLEBUFCELLNUM 10


struct BufCell
{
	unsigned long tag;
	unsigned long in_len;
	unsigned char in_buf[MAXCYCLEBUFCELLLENGHT];
};




class CCycleBuf  
{
public:
	CCycleBuf();
	virtual ~CCycleBuf();
	bool SetBuf(unsigned char * buf,unsigned long len);
	bool GetBuf(unsigned char * buf,unsigned long &len);
	long GetFreeSize();
	long GetFullSize();
protected:
	BufCell cells[CYCLEBUFCELLNUM];
	long readpos;
	long writepos;
	long readcnt;
	long writecnt;

};


class CSerial  
{
public:
	CSerial();
	virtual ~CSerial();
	bool InitWinSerial(unsigned int port,int baudRate);
	friend unsigned long  __stdcall WinCommReadFunc(void *para);
	friend unsigned long  __stdcall witimerfunc(unsigned int p1,unsigned int p2,unsigned long p3,unsigned long p4,unsigned long p5);
	virtual void OnData(unsigned char ch);
	bool WriteComm(unsigned char * buf,long size);
	virtual void On_Time_out();
	long timeouttime;
protected:
	HANDLE   m_hComDev;
	DCB   dcb;
};



class CSeal {
public:
	CSeal();
	virtual ~CSeal();
	static bool Seal(unsigned char * in_buf,long in_len,unsigned char * out_buf,long &out_len,unsigned char pri);
	void EmptyState();
	int DeSeal(unsigned char ch,unsigned char * retbuf,unsigned long &ret_len,unsigned char &pri);
protected:
	unsigned char seal_state;
	unsigned char recvbuf[512];
	unsigned long cursize;
	unsigned char crc1;
	unsigned char crc2;
	unsigned long seal_len;
	unsigned char priv;
};

class CDGPSAgent
{
public: 
	CDGPSAgent(){};
	virtual ~CDGPSAgent(){};
	virtual void on_DGPS_Data(unsigned char ch)=0;
	virtual void on_DGPS_Time_out() = 0;
};

class CDGPSCom :public CSerial
{
public:
	CDGPSCom();
	virtual ~CDGPSCom();
	virtual void OnData(unsigned char ch);
	virtual void On_Time_out();

	CDGPSAgent * dgps_ins;
};

class CWirelessNetAgent
{
public:
	CWirelessNetAgent(){};
	virtual ~CWirelessNetAgent(){};
    virtual void onWirelessDataReceive(unsigned char * data_buf,long data_size) = 0;
};


class CWiCom  :public CSerial
{
public:
	CWiCom();
	virtual ~CWiCom();
    
    
	virtual void OnData(unsigned char ch);
	virtual void On_Time_out();
	
	bool GetArriveBuf(unsigned char * buf,unsigned long & len,unsigned char & pri);
	bool SetSendBuf(unsigned char * buf,unsigned long len,unsigned char  pri);
	bool OnSendData(unsigned char *data,unsigned long size);
	void SetNetAgent(CWirelessNetAgent * nda);
	CWirelessNetAgent *NetAgent;

public:
	CSeal		seal;
	CCycleBuf	in_buf;
	CCycleBuf	out_buf;
	CCycleBuf	in_buf_l;
	CCycleBuf	out_buf_l;
protected:
	unsigned char	de_seal_ret_buf[512];
	unsigned long	de_seal_ret_len;
	unsigned char request_ins[15];
	long request_ins_len;
	void Inner_Send();
	long try_link_cnt;

};



#endif 
